﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ShearwaterLogReader
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream file = new FileStream(@"c:\temp\shearwater.dat", FileMode.Open))
            {
                byte[] bytes = new byte[file.Length];

                file.Read(bytes, 0, 131200);

                List<Dive> dives = new List<Dive>();
                Dive dc = null;

                int count = 0;

                for (int i = 0; i < bytes.Length; i += 128)
                {
                    if (bytes[i] == 0xFF && bytes[i + 1] == 0xFE)
                    {
                        count++;
                    }
                }

                {
                    int i = 0;

                    while (true)
                    {
                        // Start of Dive
                        if (bytes[i] == 0xFF && bytes[i + 1] == 0xFF)
                        {
                            dc = new Dive();

                            dc.DiveID = (int)bytes[i + 3];
                            dc.GfLow = (double)bytes[i + 4] / 100;
                            dc.GfHigh = (double)bytes[i + 5] / 100;
                            dc.SurfaceInterval = (double)bytes[i + 7];
                            dc.Units = ((int)bytes[i + 8] == 1) ? Units.Metric : Units.Imperial;
                            dc.BatteryVoltage = (double)bytes[i + 9];
                            dc.HoldCns = (double)bytes[i + 11];
                            dc.CurrentTime = (double)bytes[i + 15];
                            dc.SensorStatus = bytes[i + 16].ToString();
                            dc.SetpointLow = (double)bytes[i + 17] / 100;
                            dc.SetpointHigh = (double)bytes[i + 18] / 100;
                            dc.VersionNumber = (double)bytes[i + 19];

                            for (int c = 0; c < 5; c++)
                            {
                                if ((int)bytes[i + 20 + c] > 0)
                                {
                                    dc.GasesOC.Add(new Gas((double)bytes[i + 20 + c] / 100, (double)bytes[i + 30 + c] / 100));
                                }

                                if ((int)bytes[i + 25 + c] > 0)
                                {
                                    dc.GasesCC.Add(new Gas((double)bytes[i + 25 + c] / 100, (double)bytes[i + 35 + c] / 100));
                                }
                            }

                            dc.SwitchUpSetting = (double)bytes[i + 40];
                            dc.SwitchDownSetting = (double)bytes[i + 42];
                            dc.SwitchUpDepth = (double)bytes[i + 41];
                            dc.SwitchDownDepth = (double)bytes[i + 43];

                            dc.SingleSingle = (double)bytes[i + 44];

                            dc.GfLow1 = (double)bytes[i + 45] / 100;
                            dc.GfHigh1 = (double)bytes[i + 46] / 100;

                            #region
                            //    eeprom_data[record_position + 40]    = switch_up_setting;
                            //    eeprom_data[record_position + 41]    = (uint8_t)switch_up_depth;
                            //    eeprom_data[record_position + 42]    = switch_down_setting;
                            //    eeprom_data[record_position + 43]    = (uint8_t)switch_down_depth;
                            //    eeprom_data[record_position + 44]    = (uint8_t)o2_sensors.single_sensor; 


                            //    eeprom_data[record_position]         = 0xFF;
                            //    eeprom_data[record_position + 1]     = 0xFF;
                            //    eeprom_data[record_position + 2]     = (dive_number & 0xFF00) >> 8;
                            //    eeprom_data[record_position + 3]     = dive_number & 0x00FF;
                            //    eeprom_data[record_position + 4]     = (uint8_t)(gf_lo * 100.0);
                            //    eeprom_data[record_position + 5]     = (uint8_t)(gf_hi * 100.0);
                            //    eeprom_data[record_position + 6]     = (surface_minutes & 0xFF00) >> 8;
                            //    eeprom_data[record_position + 7]     = surface_minutes & 0x00FF;
                            //    eeprom_data[record_position + 8]     = current_units_setting; // Metric or Imperial
                            //    eeprom_data[record_position + 9]     = (uint8_t)(battery_voltage_dac * .043916); // Voltage X 10
                            //                                           hold_cns = (uint16_t)cns_percent; // From float
                            //    eeprom_data[record_position + 10]    = (hold_cns & 0xFF00) >> 8;
                            //    eeprom_data[record_position + 11]    =  hold_cns & 0x00FF;
                            //    eeprom_data[record_position + 12]    = (uint8_t)((current_time & 0xFF000000) >> 24);
                            //    eeprom_data[record_position + 13]    = (uint8_t)((current_time & 0x00FF0000) >> 16);
                            //    eeprom_data[record_position + 14]    = (uint8_t)((current_time & 0x0000FF00) >> 8);
                            //    eeprom_data[record_position + 15]    = (uint8_t)(current_time & 0x000000FF);
                            //    eeprom_data[record_position + 16]    = (uint8_t)o2_sensors.status; 
                            //    eeprom_data[record_position + 17]    = low_setpoint_setting;
                            //    eeprom_data[record_position + 18]    = high_setpoint_setting;
                            //    eeprom_data[record_position + 19]    = version_number;
                            //    eeprom_data[record_position + 20]    = fraction_o2[0];
                            //    eeprom_data[record_position + 21]    = fraction_o2[1];
                            //    eeprom_data[record_position + 22]    = fraction_o2[2];
                            //    eeprom_data[record_position + 23]    = fraction_o2[3];
                            //    eeprom_data[record_position + 24]    = fraction_o2[4];
                            //    eeprom_data[record_position + 25]    = fraction_o2[5];
                            //    eeprom_data[record_position + 26]    = fraction_o2[6];
                            //    eeprom_data[record_position + 27]    = fraction_o2[7];
                            //    eeprom_data[record_position + 28]    = fraction_o2[8];
                            //    eeprom_data[record_position + 29]    = fraction_o2[9];
                            //    eeprom_data[record_position + 30]    = fraction_he[0];
                            //    eeprom_data[record_position + 31]    = fraction_he[1];
                            //    eeprom_data[record_position + 32]    = fraction_he[2];
                            //    eeprom_data[record_position + 33]    = fraction_he[3];
                            //    eeprom_data[record_position + 34]    = fraction_he[4];
                            //    eeprom_data[record_position + 35]    = fraction_he[5];
                            //    eeprom_data[record_position + 36]    = fraction_he[6];
                            //    eeprom_data[record_position + 37]    = fraction_he[7];
                            //    eeprom_data[record_position + 38]    = fraction_he[8];
                            //    eeprom_data[record_position + 39]    = fraction_he[9];
                            //    eeprom_data[record_position + 40]    = switch_up_setting;
                            //    eeprom_data[record_position + 41]    = (uint8_t)switch_up_depth;
                            //    eeprom_data[record_position + 42]    = switch_down_setting;
                            //    eeprom_data[record_position + 43]    = (uint8_t)switch_down_depth;
                            //    eeprom_data[record_position + 44]    = (uint8_t)o2_sensors.single_sensor; 
                            //    eeprom_data[record_position + 45]    = (uint8_t)(gf_lo * 100.0);
                            //    eeprom_data[record_position + 46]    = (uint8_t)(gf_hi * 100.0);
                            #endregion


                            dives.Add(dc);

                            i += 128;

                            while (true)
                            {
                                DiveRecord dr = new DiveRecord();

                                dr.CurrentDepth = (double)bytes[i];
                                dr.FirstStopDepth = (double)bytes[i + 2];
                                dr.TTSMinutes = (double)bytes[i + 3];
                                dr.AveragePPO2 = (double)bytes[i + 6];
                                dr.FO2 = (double)bytes[i + 7] / 100;
                                dr.FHe = (double)bytes[i + 8] / 100;
                                dr.FirstStopTime = (double)bytes[i + 9];
                                dr.CurrentNoDecoLimit = (double)bytes[i + 10];

                                dr.WaterTemp = (double)bytes[i + 13];
                                dr.WaterTemp = (double)bytes[i + 13];

                                #region
                                //    eeprom_data[record_position]         = (current_depth & 0x7F00) >> 8; //7F excludes FF
                                //    eeprom_data[record_position + 1]     = current_depth & 0x00FF;
                                //    eeprom_data[record_position + 2]     = (first_stop_depth & 0x7F00) >> 8; //7F excludes FF for upload
                                //    eeprom_data[record_position + 3]     = first_stop_depth & 0x00FF;
                                //    eeprom_data[record_position + 4]     = (tts_minutes & 0xFF00) >> 8;
                                //    eeprom_data[record_position + 5]     = tts_minutes & 0x00FF;
                                //    eeprom_data[record_position + 6]     = average_ppo2;
                                //    eeprom_data[record_position + 7]     = fraction_o2[current_gas_number];
                                //    eeprom_data[record_position + 8]     = fraction_he[current_gas_number];
                                //    eeprom_data[record_position + 9]     = first_stop_time & 0x00FF;
                                //    eeprom_data[record_position + 10]    = current_ndl;

                                //    system_byte = 0;
                                //    system_byte |= (switch_needed & 0x01);
                                //    system_byte |= (current_external_setting & 0x01) << 1;
                                //    system_byte |= (current_setpoint_state & 0x01) << 2;
                                //    system_byte |= (identity_setting & 0x01) << 3;   
                                //    eeprom_data[record_position + 11]    = system_byte;
                                //    eeprom_data[record_position + 12]    = current_circuit_setting;
                                //    eeprom_data[record_position + 13]    = (sint8_t)water_temperature;
                                //    eeprom_data[record_position + 14]    = (error_acks    & 0x000000FF);
                                //    eeprom_data[record_position + 15]    = (error_flags & 0x000000FF);
                                #endregion

                                dc.DiveRecords.Add(dr);

                                if (bytes[i + 16] == 0xFF && bytes[i + 17] == 0xFE)
                                {
                                    i += 16;

                                    // skip end record
                                    i += 128;
                                    break;
                                }
                                else
                                {
                                    i += 16;
                                }
                            }
                        }



                        if (bytes[i + 1] == 0xFF && bytes[i + 2] == 0xFD)
                        {
                            //i += ;

                            //    eeprom_data[0] = 0xFF;
                            //    eeprom_data[1] = 0xFD;
                            //    eeprom_data[2] = serial_number[0];
                            //    eeprom_data[3] = serial_number[1];
                            //    eeprom_data[4] = serial_number[2];
                            //    eeprom_data[5] = serial_number[3];
                            //    eeprom_data[6] = (prom_address & 0x00FF0000) >> 16;
                            //    eeprom_data[7] = (prom_address & 0x0000FF00) >> 8;
                            //    eeprom_data[8] = prom_address & 0x000000FF;
                            //    eeprom_data[9] = checksum;
                            //    eeprom_data[10] = SOFTWARE_VERSION;
                            //    eeprom_data[11] = model;
                            //    eeprom_data[12] = LOG_VERSION;
                            //    eeprom_data[13] = 0;
                            //    eeprom_data[14] = 0;
                            //    eeprom_data[15] = 0;
                            break;
                        }

                    }
                }
            }
        }
    }

public class Dive
{
    public int DiveID { get; set; }
    public double GfLow { get; set; }
    public double GfHigh { get; set; }
    public double SurfaceInterval { get; set; }
    public Units Units { get; set; }

    public double BatteryVoltage { get; set; }
    public double HoldCns { get; set; }
    public double CurrentTime { get; set; }
    public string SensorStatus { get; set; }

    public double SetpointLow { get; set; }
    public double SetpointHigh { get; set; }
    public double VersionNumber { get; set; }

    public List<Gas> GasesOC { get; set; }
    public List<Gas> GasesCC { get; set; }


    public double SwitchUpSetting { get; set; }
    public double SwitchDownSetting { get; set; }
    public double SwitchUpDepth { get; set; }
    public double SwitchDownDepth { get; set; }
    public double SingleSingle { get; set; }

    public double GfLow1 { get; set; }
    public double GfHigh1 { get; set; }

    public List<DiveRecord> DiveRecords { get; set; }

    public Dive()
    {
        this.DiveRecords = new List<DiveRecord>();
        this.GasesOC = new List<Gas>();
        this.GasesCC = new List<Gas>();
    }
}

public class SystemInfo
{

}

public class DiveRecord
{
    public double CurrentDepth { get; set; }
    public double FirstStopDepth { get; set; }
    public double TTSMinutes { get; set; }
    public double AveragePPO2 { get; set; }
    public double FO2 { get; set; }
    public double FHe { get; set; }
    public double FirstStopTime { get; set; }
    public double CurrentNoDecoLimit { get; set; }
    public double WaterTemp { get; set; }
    public double ErrorAcks { get; set; }
    public double ErrorFlags { get; set; }
    
    public override string ToString()
    {
        return string.Format("{0}m; NDL: {1}; @ {2}m for {3}", CurrentDepth, CurrentNoDecoLimit, FirstStopDepth, FirstStopTime);
    }
}

}

//    eeprom_data[record_position]         = (current_depth & 0x7F00) >> 8; //7F excludes FF
//    eeprom_data[record_position + 1]     = current_depth & 0x00FF;
//    eeprom_data[record_position + 2]     = (first_stop_depth & 0x7F00) >> 8; //7F excludes FF for upload
//    eeprom_data[record_position + 3]     = first_stop_depth & 0x00FF;
//    eeprom_data[record_position + 4]     = (tts_minutes & 0xFF00) >> 8;
//    eeprom_data[record_position + 5]     = tts_minutes & 0x00FF;
//    eeprom_data[record_position + 6]     = average_ppo2;
//    eeprom_data[record_position + 7]     = fraction_o2[current_gas_number];
//    eeprom_data[record_position + 8]     = fraction_he[current_gas_number];
//    eeprom_data[record_position + 9]     = first_stop_time & 0x00FF;
//    eeprom_data[record_position + 10]    = current_ndl;

//    system_byte = 0;
//    system_byte |= (switch_needed & 0x01);
//    system_byte |= (current_external_setting & 0x01) << 1;
//    system_byte |= (current_setpoint_state & 0x01) << 2;
//    system_byte |= (identity_setting & 0x01) << 3;   
//    eeprom_data[record_position + 11]    = system_byte;
//    eeprom_data[record_position + 12]    = current_circuit_setting;
//    eeprom_data[record_position + 13]    = (sint8_t)water_temperature;
//    eeprom_data[record_position + 14]    = (error_acks    & 0x000000FF);
//    eeprom_data[record_position + 15]    = (error_flags & 0x000000FF);


//I think the easiest is to just send you the code that generates the logs.  
//Here are the open, update, and close log functions that are called for a dive.  
//Please note that bytes 14 and 15 of the update are internal flags of error conditions 
//and their meaning may change.

 

//static void open_log(void)

//{
//    dive_number = read_config_rom(DIVE_NUMBER_LOCATION) << 8;
//    dive_number += read_config_rom(DIVE_NUMBER_LOCATION + 1);
//    dive_number ++; 
//    update_config_rom(DIVE_NUMBER_LOCATION, (dive_number & 0xFF00) >> 8);
//    update_config_rom(DIVE_NUMBER_LOCATION + 1, dive_number & 0x00FF);
//                store_config_rom();
    
//    record_position = 0;
//    memset(eeprom_data, 0, sizeof eeprom_data);
    
//    eeprom_data[record_position]         = 0xFF;
//    eeprom_data[record_position + 1]     = 0xFF;
//    eeprom_data[record_position + 2]     = (dive_number & 0xFF00) >> 8;
//    eeprom_data[record_position + 3]     = dive_number & 0x00FF;
//    eeprom_data[record_position + 4]     = (uint8_t)(gf_lo * 100.0);
//    eeprom_data[record_position + 5]     = (uint8_t)(gf_hi * 100.0);
//    eeprom_data[record_position + 6]     = (surface_minutes & 0xFF00) >> 8;
//    eeprom_data[record_position + 7]     = surface_minutes & 0x00FF;
//    eeprom_data[record_position + 8]     = current_units_setting; // Metric or Imperial
//    eeprom_data[record_position + 9]     = (uint8_t)(battery_voltage_dac * .043916); // Voltage X 10
//                                           hold_cns = (uint16_t)cns_percent; // From float
//    eeprom_data[record_position + 10]    = (hold_cns & 0xFF00) >> 8;
//    eeprom_data[record_position + 11]    =  hold_cns & 0x00FF;
//    eeprom_data[record_position + 12]    = (uint8_t)((current_time & 0xFF000000) >> 24);
//    eeprom_data[record_position + 13]    = (uint8_t)((current_time & 0x00FF0000) >> 16);
//    eeprom_data[record_position + 14]    = (uint8_t)((current_time & 0x0000FF00) >> 8);
//    eeprom_data[record_position + 15]    = (uint8_t)(current_time & 0x000000FF);
//    eeprom_data[record_position + 16]    = (uint8_t)o2_sensors.status; 
//    eeprom_data[record_position + 17]    = low_setpoint_setting;
//    eeprom_data[record_position + 18]    = high_setpoint_setting;
//    eeprom_data[record_position + 19]    = version_number;
//    eeprom_data[record_position + 20]    = fraction_o2[0];
//    eeprom_data[record_position + 21]    = fraction_o2[1];
//    eeprom_data[record_position + 22]    = fraction_o2[2];
//    eeprom_data[record_position + 23]    = fraction_o2[3];
//    eeprom_data[record_position + 24]    = fraction_o2[4];
//    eeprom_data[record_position + 25]    = fraction_o2[5];
//    eeprom_data[record_position + 26]    = fraction_o2[6];
//    eeprom_data[record_position + 27]    = fraction_o2[7];
//    eeprom_data[record_position + 28]    = fraction_o2[8];
//    eeprom_data[record_position + 29]    = fraction_o2[9];
//    eeprom_data[record_position + 30]    = fraction_he[0];
//    eeprom_data[record_position + 31]    = fraction_he[1];
//    eeprom_data[record_position + 32]    = fraction_he[2];
//    eeprom_data[record_position + 33]    = fraction_he[3];
//    eeprom_data[record_position + 34]    = fraction_he[4];
//    eeprom_data[record_position + 35]    = fraction_he[5];
//    eeprom_data[record_position + 36]    = fraction_he[6];
//    eeprom_data[record_position + 37]    = fraction_he[7];
//    eeprom_data[record_position + 38]    = fraction_he[8];
//    eeprom_data[record_position + 39]    = fraction_he[9];
//    eeprom_data[record_position + 40]    = switch_up_setting;
//    eeprom_data[record_position + 41]    = (uint8_t)switch_up_depth;
//    eeprom_data[record_position + 42]    = switch_down_setting;
//    eeprom_data[record_position + 43]    = (uint8_t)switch_down_depth;
//    eeprom_data[record_position + 44]    = (uint8_t)o2_sensors.single_sensor; 
//    eeprom_data[record_position + 45]    = (uint8_t)(gf_lo * 100.0);
//    eeprom_data[record_position + 46]    = (uint8_t)(gf_hi * 100.0);
    
//    write_log();
//}
 
//static void update_log(void)
//{
//    eeprom_data[record_position]         = (current_depth & 0x7F00) >> 8; //7F excludes FF
//    eeprom_data[record_position + 1]     = current_depth & 0x00FF;
//    eeprom_data[record_position + 2]     = (first_stop_depth & 0x7F00) >> 8; //7F excludes FF for upload
//    eeprom_data[record_position + 3]     = first_stop_depth & 0x00FF;
//    eeprom_data[record_position + 4]     = (tts_minutes & 0xFF00) >> 8;
//    eeprom_data[record_position + 5]     = tts_minutes & 0x00FF;
//    eeprom_data[record_position + 6]     = average_ppo2;
//    eeprom_data[record_position + 7]     = fraction_o2[current_gas_number];
//    eeprom_data[record_position + 8]     = fraction_he[current_gas_number];
//    eeprom_data[record_position + 9]     = first_stop_time & 0x00FF;
//    eeprom_data[record_position + 10]    = current_ndl;
    
//    system_byte = 0;
//    system_byte |= (switch_needed & 0x01);
//    system_byte |= (current_external_setting & 0x01) << 1;
//    system_byte |= (current_setpoint_state & 0x01) << 2;
//                system_byte |= (identity_setting & 0x01) << 3;   
//    eeprom_data[record_position + 11]    = system_byte;
//    eeprom_data[record_position + 12]    = current_circuit_setting;
//    eeprom_data[record_position + 13]    = (sint8_t)water_temperature;
//    eeprom_data[record_position + 14]    = (error_acks    & 0x000000FF);
//    eeprom_data[record_position + 15]    = (error_flags & 0x000000FF);
    
//    if (record_position == 112){
//        write_log();
//        update_needed = 0;
//    }else{
//        record_position += 16;
//        update_needed = 1;
//    }  
//}
    
//static void close_log(void)
//{
//    if (update_needed){
// /       write_log;
//    }
    
//    eeprom_data[record_position]         = 0xFF;
//    eeprom_data[record_position + 1]     = 0xFE;
//    eeprom_data[record_position + 2]     = (uint8_t)((dive_number & 0xFF00) >> 8);
//    eeprom_data[record_position + 3]     = dive_number & 0x00FF;
//    eeprom_data[record_position + 4]     = (max_depth & 0xFF00) >> 8;
//    eeprom_data[record_position + 5]     = max_depth & 0x00FF;
//    eeprom_data[record_position + 6]     = (max_time & 0xFF00) >> 8;
//    eeprom_data[record_position + 7]     = max_time & 0x00FF;
//    eeprom_data[record_position + 8]     = error_history;
//    eeprom_data[record_position + 9]     = (uint8_t)(battery_voltage_dac * 0.042887); // Voltage X 10
//                                           hold_cns = (uint16_t)cns_percent; // From float
//    eeprom_data[record_position + 10]    = (hold_cns & 0xFF00) >> 8;
//    eeprom_data[record_position + 11]    =  hold_cns & 0x00FF;
//    eeprom_data[record_position + 12]    = (uint8_t)((current_time & 0xFF000000) >> 24);
//    eeprom_data[record_position + 13]    = (uint8_t)((current_time & 0x00FF0000) >> 16);
//    eeprom_data[record_position + 14]    = (uint8_t)((current_time & 0x0000FF00) >> 8);
//    eeprom_data[record_position + 15]    = (uint8_t)(current_time & 0x000000FF);
//    eeprom_data[record_position + 16]    = (uint8_t)o2_sensors.status; 
//    eeprom_data[record_position + 17]    = low_setpoint_setting;
//    eeprom_data[record_position + 18]    = high_setpoint_setting;
//    eeprom_data[record_position + 19]    = 0;
//    eeprom_data[record_position + 20]    = fraction_o2[0];
//    eeprom_data[record_position + 21]    = fraction_o2[1];
//    eeprom_data[record_position + 22]    = fraction_o2[2];
//    eeprom_data[record_position + 23]    = fraction_o2[3];
//    eeprom_data[record_position + 24]    = fraction_o2[4];
//    eeprom_data[record_position + 25]    = fraction_o2[5];
//    eeprom_data[record_position + 26]    = fraction_o2[6];
//    eeprom_data[record_position + 27]    = fraction_o2[7];
//    eeprom_data[record_position + 28]    = fraction_o2[8];
//    eeprom_data[record_position + 29]    = fraction_o2[9];
//    eeprom_data[record_position + 30]    = fraction_he[0];
//    eeprom_data[record_position + 31]    = fraction_he[1];
//    eeprom_data[record_position + 32]    = fraction_he[2];
//    eeprom_data[record_position + 33]    = fraction_he[3];
//    eeprom_data[record_position + 34]    = fraction_he[4];
//    eeprom_data[record_position + 35]    = fraction_he[5];
//    eeprom_data[record_position + 36]    = fraction_he[6];
//    eeprom_data[record_position + 37]    = fraction_he[7];
//    eeprom_data[record_position + 38]    = fraction_he[8];
//    eeprom_data[record_position + 39]    = fraction_he[9];
//    eeprom_data[record_position + 40]    = switch_up_setting;
//    eeprom_data[record_position + 41]    = (uint8_t)switch_up_depth;
//    eeprom_data[record_position + 42]    = switch_down_setting;
//    eeprom_data[record_position + 43]    = (uint8_t)switch_down_depth;
//    eeprom_data[record_position + 44]    = (uint8_t)o2_sensors.single_sensor;
//                eeprom_data[record_position + 45]    = (uint8_t)(gf_lo * 100.0);
//                eeprom_data[record_position + 46]    = (uint8_t)(gf_hi * 100.0);
 
//    if (update_needed){
//                                log_write_pending = 1;
//                }else{
//                                write_log();
//                }
//}
 
//In addition, when the log is actually transmitted to the IrDA port, the following 128 bytes  is added 
//on as the last block.  It is not part of the actual log data:
//// Send the final record
//    eeprom_data[0] = 0xFF;
//    eeprom_data[1] = 0xFD;
//    eeprom_data[2] = serial_number[0];
//    eeprom_data[3] = serial_number[1];
//    eeprom_data[4] = serial_number[2];
//    eeprom_data[5] = serial_number[3];
//    eeprom_data[6] = (prom_address & 0x00FF0000) >> 16;
//    eeprom_data[7] = (prom_address & 0x0000FF00) >> 8;
//    eeprom_data[8] = prom_address & 0x000000FF;
//    eeprom_data[9] = checksum;
//    eeprom_data[10] = SOFTWARE_VERSION;
//    eeprom_data[11] = model;
//    eeprom_data[12] = LOG_VERSION;
//    eeprom_data[13] = 0;
//    eeprom_data[14] = 0;
//    eeprom_data[15] = 0;
 

//It is important for you to know that the log ends at the “prom_address.”  It contains the 
//next address to write.  In order to scan the logs, start at the prom_address and go back 128 
//bytes at a time.  If you find an end dive record “0xFF 0xFE”, record the dive number and 
//search for the start dive record “0xFF 0xFF” for the same dive.  If you find another end 
//dive record, start to search for the matching start dive again.  When you have wrapped 
//around to prom address again, you are done.  You need to skip the last 128 bytes in the 
//file because that is the above system record.

 

//The design allows the little processor in the dive computer to efficiently bring up 
//the dives in the order of last dive first, as divers expect.

 

//There are several reasons why dives might not match up.  The battery can die during 
//a dive.  Also, the FLASH chip needs to erase a sector before starting to write, so it 
//sometimes overwrites parts of dives.  Just make sure that any dives you report have a 
//start dive and end dive with the same dive number.

 

//Also, confirm that your download is 131200 bytes.

 

//Note:  Records starting with “0xFF 0xFC” are system records and should be ignored.  
//In the future they may contain interesting events like calibrations.

 

//Bruce
